package com.ymttest.business.service.shenqi;

import java.math.BigDecimal;
import java.util.List;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.OrderBizReq;
import com.shop2cn.iapi.sqoptrade.parameter.req.PlaceOrderReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.BizVo;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderItemVo;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ChangeOrderBizStatusCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.utils.EnvSetup;

/**
 * 
 * 砍价团服务
 *
 * bizSubType  业务子类型; 砍价团(1 开团, 2 参团)
 * 
 * 业务状态 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3 组团成功)
 * 
 */
public class BargainGroupService {

	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	
	/**
	 * 神器订单开团
	 * 
	 * @param bizId
	 *            团id
	 * @throws Exception
	 * 
	 * */
	public static TradingServiceResp shenqiOpenGroup(String bizId,
			AgentProductBean productBean, boolean useVirtualStock)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, 20238699,
						productBean, (int) autotest_goodsOwnerId);

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();

		if (useVirtualStock) {
			orderItems.get(0).setProductNum(12);
			orderItems.get(0).setVirtualProductNum(2);
		}

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(1);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal totalPrice = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		Long thirdTradingId = SqTradingCallService.payOrder(userId,
				tradingId);

		// 支付网关支付回调交易
		SQPayGateWayCallService.DGSQNotifyCall(String.valueOf(thirdTradingId),
				totalPrice.subtract(BigDecimal.ZERO).toString(), (int) userId,
				BigDecimal.ZERO.toPlainString(), productBean.getCurrency());

		return placeOrderResp;
	}
	
	public static TradingServiceResp shenqiOpenGroupWithVirtualCoin(String bizId,
			AgentProductBean productBean, boolean useVirtualStock)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, productBean.getSellerId(),
						productBean, (long) productBean.getGoodsOwnerId());

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();

		if (useVirtualStock) {
			orderItems.get(0).setProductNum(12);
			orderItems.get(0).setVirtualProductNum(2);
		}

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(1);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();
		long tradingId = placeOrderResp.getTradingId();
		
		// 充值代币
		SqPretradeService.virtualCoinCharge(productBean.getGoodsOwnerId(),
				productBean.getSellerId(), useAccountAmount);

		// 支付订单
		SqTradingCallService.payOrderWithVirtualCoin(userId,
				tradingId, useAccountAmount);

		return placeOrderResp;
	}
	
	/**
	 * 神器一品多码订单开团
	 * 
	 * @param bizId
	 *            团id
	 * @throws Exception
	 * 
	 * */
	public static TradingServiceResp shenqiMultiCodeOpenGroup(String bizId,
			AgentProductBean productBean)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, 20238699,
						productBean, productBean.getGoodsOwnerId());

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();
		
		orderItems.get(0).setProductNum(102);

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(1);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal totalPrice = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		Long thirdTradingId = SqTradingCallService.payOrder(userId,
				tradingId);

		// 支付网关支付回调交易
		SQPayGateWayCallService.DGSQNotifyCall(String.valueOf(thirdTradingId),
				totalPrice.subtract(BigDecimal.ZERO).toString(), (int) userId,
				BigDecimal.ZERO.toPlainString(), productBean.getCurrency());

		return placeOrderResp;
	}
	
	/**
	 * 神器订单参团
	 * 
	 * @throws Exception
	 * 
	 * */
	public static TradingServiceResp shenqiJoinGroup(String bizId,
			AgentProductBean productBean, boolean useVirtualStock)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, 20238699,
						productBean, (int) autotest_goodsOwnerId);

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();

		if (useVirtualStock) {
			orderItems.get(0).setProductNum(12);
			orderItems.get(0).setVirtualProductNum(2);
		}

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(2);
		biz.setBizStatus(0);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal totalPrice = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		Long thirdTradingId = SqTradingCallService.payOrder(userId,
				tradingId);

		// 支付网关支付回调交易
		SQPayGateWayCallService.DGSQNotifyCall(String.valueOf(thirdTradingId),
				totalPrice.subtract(BigDecimal.ZERO).toString(), (int) userId,
				BigDecimal.ZERO.toPlainString(), productBean.getCurrency());

		return placeOrderResp;
	}
	
	public static TradingServiceResp shenqiJoinGroupWithVirtualCoin(String bizId,
			AgentProductBean productBean, boolean useVirtualStock)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, productBean.getSellerId(),
						productBean, (long) productBean.getGoodsOwnerId());

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();

		if (useVirtualStock) {
			orderItems.get(0).setProductNum(12);
			orderItems.get(0).setVirtualProductNum(2);
		}

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(2);
		biz.setBizStatus(0);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 充值代币
		SqPretradeService.virtualCoinCharge(productBean.getGoodsOwnerId(),
				productBean.getSellerId(), useAccountAmount);

		// 支付订单
		SqTradingCallService.payOrderWithVirtualCoin(userId, tradingId,
				useAccountAmount);

		return placeOrderResp;
	}
	
	/**
	 * 神器一品多码商品订单参团
	 * 
	 * @throws Exception
	 * 
	 * */
	public static TradingServiceResp shenqiMultiCodeJoinGroup(String bizId,
			AgentProductBean productBean)
			throws Exception {

		PlaceOrderReq placeOrderReq = SqTradingCallService
				.buildDGSQStandPlaceOrderReq(1234, BigDecimal.ZERO, 20238699,
						productBean, productBean.getGoodsOwnerId());

		List<OrderItemVo> orderItems = placeOrderReq.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems();
		
		orderItems.get(0).setProductNum(50);

		BizVo biz = new BizVo();

		biz.setBizId(bizId);
		biz.setBizSubType(2);
		biz.setBizStatus(0);
		biz.setBizType(1);

		placeOrderReq.setBizSource(biz);

		TradingServiceResp placeOrderResp = SqTradingCallService
				.callPlaceOrderService(placeOrderReq);

		long tradingId = placeOrderResp.getTradingId();
		BigDecimal totalPrice = placeOrderResp.getPayableAmount();
		long userId = placeOrderResp.getUserId();

		// 支付订单
		Long thirdTradingId = SqTradingCallService.payOrder(userId,
				tradingId);

		// 支付网关支付回调交易
		SQPayGateWayCallService.DGSQNotifyCall(String.valueOf(thirdTradingId),
				totalPrice.subtract(BigDecimal.ZERO).toString(), (int) userId,
				BigDecimal.ZERO.toPlainString(), productBean.getCurrency());

		return placeOrderResp;
	}
	
	/**
	 * 砍价团服务通知交易系统，团状态接口
	 * 
	 * @param bizId
	 *            团id
	 * @param orderId
	 *            订单号
	 * @param bizSubType
	 *            砍价团(1 开团, 2 参团)
	 * @param bizStatus
	 *            状态 砍价团(-3 组团失败, -2 参团失败, -1 开团失败, 0 默认砍价团, 1 开团成功, 2 参团成功, 3
	 *            组团成功)
	 * */
	public static void notifyOrderBizStatus(String bizId, long orderId,
			int bizSubType, int bizStatus) {

		OrderBizReq orderBizReq = new OrderBizReq();
		ChangeOrderBizStatusCall changeorderbizstatusCall = new ChangeOrderBizStatusCall();

		try {
			BizVo bizVo = new BizVo();

			bizVo.setBizStatus(bizStatus);
			bizVo.setBizSubType(bizSubType);
			bizVo.setBizType(1);
			bizVo.setOrderId(orderId);
			bizVo.setBizId(bizId);

			orderBizReq.setBizVos(Lists.newArrayList(bizVo));

			changeorderbizstatusCall.setData(false, orderBizReq);
			changeorderbizstatusCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		
		BargainGroupService.notifyOrderBizStatus("418027411", 7760548699L, 2, 3);
	}
}
